package com.rc.saas.mini.controller.tool;


import com.rc.saas.base.MinipBaseController;
import com.rc.saas.mini.common.base.DataTResponse;
import com.rc.saas.mini.component.UploadComponent;
import com.rc.saas.mini.minip.vo.LoginInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author: zcg
 * @CreateDate: 2022/2/10 15:01
 */

@Api(tags = "02.统一上传接口")
@RestController
@RequestMapping(value = "/upload")
public class UploadController extends MinipBaseController {

    private static Logger logger = LogManager.getLogger(UploadController.class);

    @Autowired
    private UploadComponent uploadComponent;


    /**
     * erp 又拍云统一上传
     *
     * @param request
     * @param
     * @return
     */
    @PostMapping("/uploadFile")
    @ApiOperation(value = "01.上传图片文件", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uploadFile", value = "File文件", required = true)
    })
    public DataTResponse<Map> uploadFile(HttpServletRequest request, @RequestParam(value = "uploadFile", required = false) MultipartFile uploadFile) {
        LoginInfoVo loginInfoVo = readLoginInfo(request);
        if (loginInfoVo == null) {
            return DataTResponse.FAIL_LOGIN();
        }
        String name = uploadComponent.uploadFile(loginInfoVo.getTenantCode(), loginInfoVo.getMemberId(), uploadFile);
        if (name != null) {
            HashMap<String, String> data = new HashMap<>();
            data.put("url", name);
            return DataTResponse.SUCCESS_DATA(data);
        }
        return DataTResponse.FAIL();
    }

    @PostMapping("/deleteFile")
    @ApiOperation(value = "02.删除文件", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "path", value = "路径", required = true)
    })
    public DataTResponse<String> deleteFile(HttpServletRequest request, @RequestParam(value = "path") String path) {
        LoginInfoVo loginInfoVo = readLoginInfo(request);
        if (loginInfoVo == null) {
            return DataTResponse.FAIL_LOGIN();
        }
        if (getUrlMemberId(loginInfoVo.getTenantCode(), path).equals(loginInfoVo.getMemberId().toString())) {
            if (uploadComponent.deleteFile(path)) {
                logger.info("用户:{},{}删除了:{}", loginInfoVo.getMemberId(), loginInfoVo.getNickname(), path);
                return DataTResponse.SUCCESS();
            }
        } else {
            logger.info("用户:{},{}无法删除错误:{}", loginInfoVo.getMemberId(), loginInfoVo.getNickname(), path);
        }
        return DataTResponse.FAIL();
    }


    /**
     * 正则表达式获取memberId
     *
     *
     */
    public static String getUrlMemberId(String tenantCode, String qrCode) {
        Pattern pattern = Pattern.compile(tenantCode + "/(\\S+)" + "/image/");
        Matcher matcher = pattern.matcher(qrCode);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return null;
    }

    public static void main(String[] args) {
        getUrlMemberId("MA4T4JPE8", "https://rc-charge.oss-cn-guangzhou.aliyuncs.com/MA4T4JPE8/127875613/image/2022051716545322.png");
    }
}
